package com.novellotus.iov.carvice.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.utils.TimeUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2017/10/10.
 */

public class TimeAdapter extends RecyclerView.Adapter<TimeAdapter.TimeHolder> {

    List<Long> timeList;

    private int clickPosition = 0;
    private TimeChooseInterface timeChooseInterface;

    public TimeAdapter(List<Long> timeList, TimeChooseInterface timeChooseInterface) {
        this.timeList = timeList;
        this.timeChooseInterface = timeChooseInterface;
    }

    @Override
    public TimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_time_itme, parent, false);
        return new TimeHolder(view);
    }

    @Override
    public void onBindViewHolder(TimeHolder holder, final int position) {
        holder.tv_time.setText(TimeUtils.stampTotime(timeList.get(position) + ""));
        if (position == clickPosition) {
            holder.tv_time.setBackgroundResource(R.drawable.layout_time_choose);
            holder.tv_time.setTextColor(Color.parseColor("#ffffff"));
        } else {
            holder.tv_time.setBackgroundResource(R.drawable.layout_time);
            holder.tv_time.setTextColor(Color.parseColor("#333333"));
        }
        holder.tv_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickPosition = position;
                timeChooseInterface.chooseTime(TimeUtils.stampTotime(timeList.get(position) + ""));
//              timeChooseInterface.chooseTime(timeList.get(position) + "");
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return timeList.size();
    }


    class TimeHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_time)
        TextView tv_time;

        TimeHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface TimeChooseInterface {
        void chooseTime(String time);
    }
}
